package edu.ads.bookloan

class LoanHistory {
	InventoryItem inventoryItem
	Date dateCreated
	Date expectedReturnDate
	Date dateReturned
	Person person
	int rentalDays = 4
	
    static constraints = {
		dateReturned nullable: true
		expectedReturnDate nullable: true
    }
	
	String toString(){
		inventoryItem + "~RENTAL_DATE=" + dateCreated + "~RENTAL_DAYS=" + rentalDays
	} 
	
	
	def beforeInsert() {
		Calendar cal = GregorianCalendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND,0);
		Date currentDate = cal.getTime();
		expectedReturnDate = currentDate + rentalDays
	}
	
	def afterInsert(){
		inventoryItem.inventoryItemStatus = InventoryItemStatus.findByStatus('Rented')
		inventoryItem.save(failOnError: true)
	}
}
